<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="乡镇名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'townshipName', validatorRules.townshipName]" placeholder="请输入乡镇名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="主任姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'directorName', validatorRules.directorName]" placeholder="请输入服务站主任姓名"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="主任职务" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'directorPosition', validatorRules.directorPosition]" placeholder="请输入服务站主任职务"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12"  pull="">
<!--          <a-col :lg="14" offset="-8" pull="8">-->
            <a-form-item label="主任电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'directorPhone', validatorRules.directorPhone]" placeholder="请输入服务站主任联系方式"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="分管姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'leaderName', validatorRules.leaderName]" placeholder="请输入分管领导姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="分管职务" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'leaderPosition', validatorRules.leaderPosition]" placeholder="请输入分管领导职务"></a-input>
            </a-form-item>
          </a-col>
        </a-row>


        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="分管电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'leaderPhone', validatorRules.leaderPhone]" placeholder="请输入分管领导联系方式"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">

          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="14" >
            <a-form-item label="贫困户总户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="[ 'poorHouseholdsTotal', validatorRules.poorHouseholdsTotal]" placeholder="请输入贫困户总户数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">

          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="14"  >
            <a-form-item label="非贫困户总户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="[ 'nonPoorHouseholdsTotal', validatorRules.nonPoorHouseholdsTotal]" placeholder="请输入非贫困户总户数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">

          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="14"  >
            <a-form-item label="贫困评级户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="[ 'poorRated', validatorRules.poorRated]" placeholder="请输入贫困评级户数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">

          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="14" >
            <a-form-item label="非贫困评级户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="[ 'nonPoorRated', validatorRules.nonPoorRated]" placeholder="请输入非贫困评级户数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="14">
            <a-form-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  例如:116.360575
                </template>
                <a-input v-decorator="[ 'longitude', validatorRules.longitude]" placeholder="请输入经度"></a-input>
              </a-tooltip>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="14">
            <a-form-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  例如:40.013812
                </template>
                <a-input v-decorator="[ 'latitude', validatorRules.latitude]" placeholder="请输入纬度"></a-input>
              </a-tooltip>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
          </a-col>
        </a-row>

        <!--<a-form-item label="贫困户数 评级过所占百分比" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'poorPersent', validatorRules.poorPersent]" placeholder="请输入贫困户数 评级过所占百分比"></a-input>
        </a-form-item>
          
        <a-form-item label="非贫困  未评级所占百分比" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'nonPoorPersent', validatorRules.nonPoorPersent]" placeholder="请输入非贫困  未评级所占百分比"></a-input>
        </a-form-item>
          
        <a-form-item label="非贫困户未评级人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'nonPoorNonRated', validatorRules.nonPoorNonRated]" placeholder="请输入非贫困户未评级人数" style="width: 100%"/>
        </a-form-item>
          
        <a-form-item label="贫困户未评级人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'poorNonRated', validatorRules.poorNonRated]" placeholder="请输入贫困户未评级人数" style="width: 100%"/>
        </a-form-item>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  
  export default {
    name: "ServiceStationModal",
    components: { 
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:900,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        townshipName:{rules: [{ required: true, message: '服务站名称不能为空'}]},
        leaderName:{},
        leaderPosition:{},
        leaderPhone:{},
        directorName:{rules: [{ required: true, message: '服务站扶贫工作负责人不能为空'}]},
        directorPosition:{rules: [{ required: true, message: '服务站扶贫工作负责人职务不能为空'}]},
        directorPhone:{rules: [{ required: true, message: '服务站扶贫工作负责人电话不能为空'}]},
        poorHouseholdsTotal:{},
        nonPoorHouseholdsTotal:{},
        poorRated:{},
        nonPoorRated:{},
        poorPersent:{},
        nonPoorPersent:{},
        nonPoorNonRated:{},
        poorNonRated:{},
        },
        url: {
          add: "/financialservices/serviceStation/add",
          edit: "/financialservices/serviceStation/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'townshipName','leaderName','leaderPosition','leaderPhone','directorName','directorPosition','directorPhone','poorHouseholdsTotal','nonPoorHouseholdsTotal','poorRated','nonPoorRated','poorPersent','nonPoorPersent','nonPoorNonRated','poorNonRated','longitude','latitude'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'townshipName','leaderName','leaderPosition','leaderPhone','directorName','directorPosition','directorPhone','poorHouseholdsTotal','nonPoorHouseholdsTotal','poorRated','nonPoorRated','poorPersent','nonPoorPersent','nonPoorNonRated','poorNonRated','longitude','latitude'))
      }
      
    }
  }
</script>